package com.example.schoolexam2.controller;


import com.example.schoolexam2.dto.EmployeeDto;
import com.example.schoolexam2.entity.Employee;
import com.example.schoolexam2.enums.ResultEnum;
import com.example.schoolexam2.service.EmployeeService;
import com.example.schoolexam2.utils.fileFormatUtils;
import com.example.schoolexam2.vo.ResultVO;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService service;

    @PostMapping("/employeeIncreases")
    ResultVO<Integer> employeeIncreases(@RequestPart MultipartFile file) {
        if (file.isEmpty()) {
            System.out.println("文件为空");
            //没有上传文件
            return new ResultVO(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
        }
        int RequestType = fileFormatUtils.excelType(file);
        if (RequestType == 2) {
            return new ResultVO(500, "格式不正确", null);
        }
        try {
            int token = service.employeeIncreases(file, RequestType);
            if (token != 0) {
                return new ResultVO<>(ResultEnum.IMPORT_SUCCESS.getCode(), ResultEnum.IMPORT_SUCCESS.getMessage(), token);
            } else {
                return new ResultVO<>(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/employeePersonal")
    ResultVO<Employee> employeePersonal(String employeeId) {
        Employee token = service.employeePersonal(employeeId);
        ResultVO<Employee> resultVO;
        if (token != null) {
            resultVO = new ResultVO<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), token);
        } else {
            resultVO = new ResultVO<>(ResultEnum.REQUEST_PARAM_ERROR.getCode(), ResultEnum.REQUEST_PARAM_ERROR.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/employeeDelete")
    ResultVO<Integer> employeeDelete(String employeeId, int isUser) {
        int token = service.employeeDelete(employeeId, isUser);
        ResultVO<Integer> resultVO;
        if (token != 0) {
            resultVO = new ResultVO<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), token);
        } else {
            resultVO = new ResultVO<>(ResultEnum.REQUEST_PARAM_ERROR.getCode(), ResultEnum.REQUEST_PARAM_ERROR.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/employeeIncrease")
    ResultVO<Integer> employeeIncrease(EmployeeDto employeeDto) {
        int token = service.employeeIncrease(employeeDto);
        ResultVO<Integer> resultVO;
        if (token != 0) {
            resultVO = new ResultVO<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), token);
        } else {
            resultVO = new ResultVO<>(ResultEnum.REQUEST_PARAM_ERROR.getCode(), ResultEnum.REQUEST_PARAM_ERROR.getMessage(), null);
        }
        return resultVO;
    }

    @RequestMapping("/employeeUpdate")
    ResultVO<Integer> employeeUpdate(EmployeeDto dto) {
        int token = service.employeeUpdate(dto);
        ResultVO<Integer> resultVO;
        if (token != 0) {
            resultVO = new ResultVO<>(ResultEnum.MODIFY_SUCCESS.getCode(), ResultEnum.MODIFY_SUCCESS.getMessage(), token);
        } else {
            resultVO = new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/employeeFuzzy")
    ResultVO<List<Employee>> employeeFuzzy(String Fuzzy) {
        List<Employee> token = service.employeeFuzzy(Fuzzy);
        ResultVO<List<Employee>> resultVO;
        if (token != null) {
            resultVO = new ResultVO<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), token);
        } else {
            resultVO = new ResultVO<>(ResultEnum.FALL.getCode(), ResultEnum.FALL.getMessage(), null);
        }
        return resultVO;
    }
}
